package com.tanhua.bg.controller;


import cn.hutool.core.util.ObjectUtil;
import com.tanhua.bg.vo.CompareResult;
import com.tanhua.bg.vo.DashResult;
import com.tanhua.bg.service.DashboardService;
import com.tanhua.bg.vo.TotalDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("dashboard")
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    @GetMapping("distribution")
    public ResponseEntity<DashResult> distribution(@RequestHeader("Authorization")String token,
                                                   @RequestParam("sd") Long sd, @RequestParam("ed") Long ed){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date startDate = new Date(sd);
//            Date endDate = new Date(ed);

            String startTimeStr = "2019-08-02 16:44:23";
            Date startTime = sdf.parse(startTimeStr);
            String endTimeStr = "2019-09-01 16:44:23";
            Date endTime = sdf.parse(endTimeStr);
            DashResult dashResult = dashboardService.distribution(startTime,endTime);
            return ResponseEntity.ok(dashResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    @GetMapping("users")
    public ResponseEntity<CompareResult> queryUsersData(@RequestHeader("Authorization") String token, @RequestParam("sd") Long sd,
                                                        @RequestParam("ed") Long ed, @RequestParam("type")String type){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTimeStr = "2019-08-02 16:44:23";
            Date startTime = sdf.parse(startTimeStr);
            String endTimeStr = "2019-09-01 16:44:23";
            Date endTime = sdf.parse(endTimeStr);
            CompareResult compareResult = dashboardService.queryUsersData(startTime, endTime, type);
            return ResponseEntity.ok(compareResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("summary")
    public ResponseEntity<TotalDataVo> queryTotal(@RequestHeader("Authorization") String token){
        TotalDataVo totalDataVo = dashboardService.queryTotal();
        if (ObjectUtil.isNotEmpty(totalDataVo)){
            return ResponseEntity.ok(totalDataVo);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
